import Koa from 'koa'
import Router from 'koa-router'
import bodyParser from 'koa-bodyparser'
import cors from '@koa/cors'

import { registerRouter } from './router/router.js'
import { sync } from './model/Book.js'
import { usersync } from './model/User.js'
import { coursesync } from './model/Course.js'
import { chaptersync } from './model/Chapter.js'

const app = new Koa()
const router = new Router()

//解析前端的数据
app.use(bodyParser())

//注册路由
registerRouter(router)

//解决跨域
app.use(cors())

//同步数据库
sync()
usersync()
coursesync()
chaptersync()

//一个中间件
// app.use(async (ctx, next) => {
//   ctx.body = "123"
//   await next()
// })

//自动注册路由
app.use(router.routes())

app.listen(8000)
console.log('http://localhost:8000');
